package checkcode;

import configuration.RobConfiguration;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class checkTool extends Application {
    private int doneCount = 0;
    private String nowFStr = "";
    private ImageView imgV;
    private Image img;

    public void start(Stage primaryStage) {
        Stage splash = new Stage();
        File f = new File(RobConfiguration.IMG_PNG_1K);
        File[] files = f.listFiles();

        VBox root = new VBox(5);

        nowFStr = files[doneCount].getName();
        String URL = files[doneCount].getAbsoluteFile().toURI().toString();
        img = new Image(URL);
        imgV = new ImageView(img);
        doneCount++;

        Label ocrL = new Label(nowFStr);
        ocrL.setPrefSize(200, 20);
        TextField inputTF = new TextField();

        root.getChildren().addAll(imgV, ocrL, inputTF);
        root.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.ENTER) {
                    String name = inputTF.getText(); //输入 修改验证码名字
                    inputTF.setText("");
                    File file = new File(RobConfiguration.IMG_PNG_1K + nowFStr);
                    File aimA = new File(RobConfiguration.IMG_PNG_TEST + name + ".png");
                    try {
                        Files.copy(file.toPath(), aimA.toPath());
                        file.delete();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    nowFStr = files[doneCount].getName();
                    String URL = files[doneCount].getAbsoluteFile().toURI().toString();
                    img = new Image(URL);
                    imgV.setImage(img);
                    ocrL.setText(nowFStr);
                    doneCount++;
                }
            }
        });

        Scene scene = new Scene(root);
        splash.setScene(scene);
        splash.setTitle("Src");
        splash.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}